﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using RoadLaneDetection;
using Counter;
using BitmapProcessing;

namespace CarAutomation
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            WebcamSelection webcam = new WebcamSelection(this);
            webcam.ShowDialog();
            InitializeComponent();
            StartWebcam();
        }

        public VideoCaptureDevice finalVideoSource;
        bool isVideoRunning = false;
        public bool IsVideoRunning { get { return isVideoRunning; } }

        private void StartWebcam()
        {
            UpdateWebcamSettings();
            try
            {
                finalVideoSource.NewFrame += new NewFrameEventHandler(
                    FinalVideoSource_NewFrame);
                finalVideoSource.Start();
            }
            finally
            {
                isVideoRunning = finalVideoSource.IsRunning;
            }
        }

        public void UpdateWebcamSettings()
        {
        }

        private void StopWebcam()
        {
            try
            {
                if (finalVideoSource.IsRunning)
                {
                    finalVideoSource.Stop();
                }
            }
            catch (NullReferenceException e) 
            { MessageBox.Show("Error topping video." + e.Message); }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            StopWebcam();
            Environment.Exit(0);
        }

        Bitmap newFrame;
        QueryPerfCounter fpsTimer = new QueryPerfCounter();
        QueryPerfCounter durationTimer = new QueryPerfCounter();
        private void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            durationTimer.Start();
            newFrame = (Bitmap)eventArgs.Frame.Clone();
            newFrame.RotateFlip(RotateFlipType.Rotate180FlipY); //miror image
            RoadLane.Detect(newFrame, 0.80);


            if (fpsTimer.IsRunning)
            {
                fpsTimer.Stop();
                fpsTimer.DurationPerIteration();
                fpsTimer.Start();
            }
            else
            {
                fpsTimer.Start();
            }

            durationTimer.Stop();
            durationTimer.DurationPerIteration();
            DrawFrameRate();
            videoPictureBox.Image = newFrame;
        }

        readonly int maxFrameRate = 72;
        private void DrawFrameRate()
        {
            if (fpsTimer.FPS < maxFrameRate)
            {
                BitmapDraw.WriteString(newFrame,
                    fpsTimer.FPSstring + " " + durationTimer.DurationInMS.ToString("00.0") + " ms",
                    Color.DarkRed, new Point(3, 3));
            }
            else
            {
                BitmapDraw.WriteString(newFrame,
                    maxFrameRate.ToString("00.0") + " FPS " + fpsTimer.DurationInMS.ToString("00.0") + " ms",
                    Color.DarkRed, new Point(3, 3));
            }
        }

    }
}
